package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.work.domain.WorkStage;
import com.ruoyi.work.service.IWorkStageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: MMDZ
 * @Desc: TODO 开发阶段
 */
@RestController
@RequestMapping("/work/stage")
public class WorkStageController extends BaseController {

    @Autowired
    private IWorkStageService workStageService;

    /**
     * 获取阶段列表
     */
    @PreAuthorize("@ss.hasPermi('work:stage:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkStage stage) {
        startPage();
        List<WorkStage> list = workStageService.selectStageList(stage);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('work:stage:export')")
    @Log(title = "项目开发阶段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkStage workStage) {
        List<WorkStage> list = workStageService.selectStageList(workStage);
        ExcelUtil<WorkStage> util = new ExcelUtil<WorkStage>(WorkStage.class);
        util.exportExcel(response, list, "项目开发阶段数据");
    }

    /**
     * 详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:stage:query')")
    @GetMapping(value = "/{stageId}")
    public AjaxResult getInfo(@PathVariable("stageId") Long stageId) {
        return AjaxResult.success(workStageService.selectStageById(stageId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('work:stage:add')")
    @Log(title = "新增项目开发阶段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkStage workStage) {
        if (UserConstants.NOT_UNIQUE.equals(workStageService.checkStageNameUnique(workStage))) {
            return AjaxResult.error("新增项目开发阶段'" + workStage.getStageName() + "'失败，名称已存在");
        }
        workStage.setCreateBy(getUsername());
        return toAjax(workStageService.insertStage(workStage));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('work:stage:edit')")
    @Log(title = "修改项目开发阶段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkStage workStage) {
        if (UserConstants.NOT_UNIQUE.equals(workStageService.checkStageNameUnique(workStage))) {
            return AjaxResult.error("修改岗位'" + workStage.getStageName() + "'失败，名称已存在");
        }
        workStage.setUpdateBy(getUsername());
        return toAjax(workStageService.updateStage(workStage));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('work:stage:remove')")
    @Log(title = "删除项目开发阶段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stageIds}")
    public AjaxResult remove(@PathVariable Long[] stageIds) {
        return toAjax(workStageService.deleteStageByIds(stageIds));
    }

    /**
     * 选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<WorkStage> stages = workStageService.selectStageAll();
        return AjaxResult.success(stages);
    }
}
